package yw.substart.example.exception;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.NoHandlerFoundException;
import yw.substart.example.util.MessageUtil;

/**
 * @author lwq 2018/5/29
 * @comment
 */
@ControllerAdvice
@RestController
public class ExcptionHandler implements ErrorController {

    private Logger logger = LoggerFactory.getLogger(ExcptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @RequestMapping("/error")
    public JSON defaultErrorHandler(Exception ex) throws Exception {
        logger.error(ex.getMessage(), ex);
        String result = "";
        if (ex instanceof BindException) {
            BindException bindException = (BindException) ex;
            for (FieldError fieldErro : bindException.getBindingResult().getFieldErrors()) {
                //result += "【" + fieldErro.getField() + ":" + fieldErro.getDefaultMessage() + "】";
                result += "【" + fieldErro.getDefaultMessage() + "】";
            }
        } else if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex;
            for (FieldError fieldErro : methodArgumentNotValidException.getBindingResult().getFieldErrors()) {
                //result += "【" + fieldErro.getField() + ":" + fieldErro.getDefaultMessage() + "】";
                result += "【" + fieldErro.getDefaultMessage() + "】";
            }
        } else if (ex instanceof HttpRequestMethodNotSupportedException) {
            HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException = (HttpRequestMethodNotSupportedException) ex;
            result = httpRequestMethodNotSupportedException.getMessage();
        } else if (ex instanceof NoHandlerFoundException) {
            result = "无效的请求地址";
        } else if (ex instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException missingServletRequestParameterException = (MissingServletRequestParameterException) ex;
            result = "缺少必要的参数[" + missingServletRequestParameterException.getMessage() + "]";
        } else {
            return MessageUtil.defaultReturnMessage(false, ex.getMessage());
        }
        return MessageUtil.defaultReturnMessage(false, result);
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
